<?php
// 应用公共文件
use \Firebase\JWT\JWT;


/**
 * 客服专员的uid
 * @return string
 */
function serviceUid()
{
    return '9527';
}

/**
 * 生成token
 * @param int $user_id
 * @return string
 */
function signToken(int $user_id)
{
    $token = [
        "iat" => time(),      //签发时间
        "exp" => strtotime("+180 minutes"), //token 过期时间戳
        "data" => [
            'user_id' => $user_id,
        ]
    ];

    $jwt = JWT::encode($token, config('utils.jwt.key'), "HS256");  //根据参数生成了 token
    return $jwt;
}

/**
 * 验证token
 * @param string $token
 * @return array
 */
function checkToken(string $token)
{
    try {
        $decoded = JWT::decode($token, config('utils.jwt.key'), array('HS256'));
        return ['code' => 1, 'msg' => 'success', 'data' => (array)$decoded];
    } catch (\Exception $e) {
        return ['code' => 0, 'msg' => $e->getMessage(), 'data' => []];
    }
}
